#! /bin/bash
export LANG=en_US.UTF-8

FLAGE=
VALUE=0
SPACEMINVALUE=
SPACEMAXVALUE=
INODEMINVALUE=
INODEMAXVALUE=
CURNUM=				#当前的性能参数
TOTALSPACE=

WranResultStr=		#最后形成的告警字符串
ErrorResultStr=		#最后形成的错误字符串
DataResultStr=		#性能呢个参数字符串
QUOTASTR=
SPACEMIN=
SPACEMAX=
INODEMIN=
INODEMAX=


StateDesStr=
MsgContent=
Remark=
ExitCode=0

quota=

function gotErr(){
	if [ "$1" -eq 1 ];then 
		if [[ "$StateDesStr" != "" ]];then
			StateDesStr=$StateDesStr";"
		fi
		StateDesStr=$StateDesStr$2"告警"
		
		if [[ "$Remark" != "" ]];then
			Remark=$Remark";"
		fi
		Remark=$Remark$WranResultStr
		
		if [[ "$MsgContent" != "" ]];then
			MsgContent=$MsgContent";"
		fi
		MsgContent=$MsgContent$2"告警,"${WranResultStr}
	elif [ "$1" -eq 2 ];then 
		
		if [[ "$StateDesStr" != "" ]];then
			StateDesStr=$StateDesStr";"
		fi
		StateDesStr=$StateDesStr$2"紧急"
		
		if [[ "$Remark" != "" ]];then
			Remark=$Remark";"
		fi
		Remark=$Remark${WranResultStr}${ErrorResultStr}
		
		if [[ "$MsgContent" != "" ]];then
			MsgContent=$MsgContent";"
		fi
		MsgContent=$MsgContent$2"紧急,"${WranResultStr}${ErrorResultStr}
	elif [ "$1" -eq 3 ];then
		if [[ $MsgContent -ne "" ]];then
			MsgContent=$MsgContent";"
		fi
		MsgContent=$MsgContent"无法获取到"$2"指标,请检查监控脚本和运维平台配置"

		if [[ "$StateDesStr" != "" ]];then
			StateDesStr=$StateDesStr";"
		fi
		StateDesStr=$StateDesStr"无法获取到"$2"指标,请检查监控脚本和运维平台配置"
	fi
	
	if [[ $ExitCode < $1 ]];then
		ExitCode=$1
	fi
}


function analysisSpaceStat(){
 	CURNUM=$(df -hP  | grep $FLAGE | head -n 1 | awk -F ' ' '{print $5}'  | sed "s/[^0-9\.]//g")	
	#MINVALUE=$(echo "$SPACEMINVALUE"| awk '{print 100-$1}')
	#MAXVALUE=$(echo "$SPACEMAXVALUE"| awk '{print 100-$1}')
	MINVALUE=$(echo "$SPACEMINVALUE"| awk '{print $1}')
	MAXVALUE=$(echo "$SPACEMAXVALUE"| awk '{print $1}')
	parseFileds "${FLAGE}" "space" $MINVALUE $MAXVALUE $SPACEMINVALUE $SPACEMAXVALUE
    
	
}

function analysisInodeStat(){
 	CURNUM=$(df -iP  | grep $FLAGE | head -n 1 | awk -F ' ' '{print $5}'  | sed "s/[^0-9\.]//g")	
	MINVALUE=$(echo "$INODEMINVALUE"| awk '{print 100-$1}')
	MAXVALUE=$(echo "$INODEMAXVALUE"| awk '{print 100-$1}')
	parseFileds "${FLAGE}" "inode" $MINVALUE $MAXVALUE $INODEMINVALUE $INODEMAXVALUE
}


#判断以上三个值并拼接结果字符串
function parseFileds(){
	val1=$(echo "$CURNUM $3"| awk '{if($1<=$2){print 1}else{print 0}}')
	val2=$(echo "$CURNUM $4"| awk '{if($1<=$2){print 1}else{print 0}}')
	
	if [[ "$DataResultStr" != "" ]];then
		DataResultStr=$DataResultStr" "
	fi
	DataResultStr="$DataResultStr$2_$1=${CURNUM}%;$3%;$4%;0;0"
	if [ $val1 -eq 1 ];then
		return 0
	elif [[ $val1 -eq 0 && $val2 -eq 1 ]];then
		WranResultStr="${WranResultStr}$2_$1"
		QUOTASTR="${QUOTASTR}$2_$1=$2_$1当前值为${CURNUM}%超过告警值${3}% "

		if [[ ${quota} != "" ]]; then
			quota="${quota},"
		fi

		quota="${quota}$2_$1"

		return 1
	else
		ErrorResultStr="${ErrorResultStr}$1_$2"
		QUOTASTR="${QUOTASTR}$2_$1=$2_$1当前值为${CURNUM}%超过紧急值${4}% "

		if [[ ${quota} != "" ]]; then
			quota="${quota},"
		fi

		quota="${quota}$2_$1"

		return 2
	fi
}

#分析结果 给出给出状态
function analysisResult(){
	if [[ "$WranResultStr" == "" && "$ErrorResultStr" == "" ]];then
		if [[ $1 -eq 1 ]];then
			gotErr 0 "磁盘空间"
		else
			gotErr 0 "文件索引数"
		fi
	elif [[ "$ErrorResultStr" == "" ]];then
		if [[ $1 -eq 1 ]];then

			WranResultStr="${WranResultStr}目录超过告警值${SPACEMIN}%"
			gotErr 1 "磁盘空间"
		else
			
			WranResultStr="${WranResultStr}文件索引数超过告警值${INODEMIN}%"
			gotErr 1 "文件索引数"
		fi
	else
		if [[ $1 -eq 1 ]];then
			if [[ "$WranResultStr" != "" ]]; then
				WranResultStr="${WranResultStr}目录超过告警值${SPACEMIN}%"
			fi
			ErrorResultStr="${ErrorResultStr}目录超过紧急值${SPACEMAX}%"
			gotErr 2 "磁盘空间"
		else
			if [[ "$WranResultStr" != "" ]]; then
				WranResultStr="${WranResultStr}文件索引数超过告警值${INODEMIN}%"
			fi
			ErrorResultStr="${ErrorResultStr}文件索引数超过紧急值${INODEMAX}%"
			gotErr 2 "文件索引数"
		fi
	fi
}

#输出错误信息并且退出程序
function showHelp(){
	if [ "$1" != "" ];then
		echo "请输入$1"
	fi
	echo "check_disk.sh 可以监听本地的磁盘的状态 参数如下"
	echo "check_disk.sh [-w 磁盘告警百分比,文件索引告警百分比] [-c 磁盘紧急百分比,文件索引紧急百分比] [-p <str>]"
	echo "-w 表示监控警告的值"
	echo "-c 表示异常值"
	echo "-p <str> 特征字符串 表示监控哪个硬盘挂载点 如/data"
	exit 1
}

#检查参数是不是数字
function isNumber(){
	isNum=$(echo "$1" | awk '{print($0~/^([0-9])+[\.]?([0-9])?$/)?1:0}')
	return $isNum
}

function isLarge(){

	isNumber $1
	
	if [[ $? -eq 0 ]]; then
		return 1
	fi

	isNumber $2
	if [[ $? -eq 0 ]]; then
		return 1
	fi

	isLarge=$(echo "$1 $2"| awk '{if($1>=$2){print 1}else{print 0}}')
	
	if [ $isLarge -eq 0 ];then
		return 0
	else
		return 1
	fi
}

function checkARG(){
	if [[ "$2" != "" && "$3" != "" ]];then
		isLarge "$3" "$2"
		if [[ $? -eq 1 ]]; then
			return 1
		else
			$1=1
		fi
	fi
	return 0
}

#检查参数是否合法
#即参数是否是小数和整数
#对于守护进程的选择 这个值可以不验证
function checkALL(){
	if [[ "$SPACEMAXVALUE" != "" || "$SPACEMINVALUE" != "" ]];then
		if [[ "$SPACEMAXVALUE" == "" || "$SPACEMINVALUE" == "" ]]; then
			return 1
		fi

		isLarge "$SPACEMINVALUE" "$SPACEMAXVALUE"
		
		if [[ $? -eq 1 ]]; then
			return 1
		fi
		
		#isLarge "$INODEMINVALUE" "$INODEMAXVALUE"
		#if [[ $? -eq 1 ]]; then
		#	return 1
		#fi
		
		return 0
	fi

	return 2
}


function parseARG(){
	#it mean warning
	if [ $1 -eq 1 ];then
 		SPACEMINVALUE=$(echo $2 | awk -F ',' '{print $1}' | sed "s/[^0-9]//g")
		SPACEMIN=$(echo "$SPACEMINVALUE"| awk '{print $1}')
		#INODEMINVALUE=$(echo $2 | awk -F ',' '{print $2}' | sed "s/[^0-9]//g")
		#INODEMIN=$(echo "$INODEMINVALUE"| awk '{print 100-$1}')
	else
 		SPACEMAXVALUE=$(echo $2 | awk -F ',' '{print $1}' | sed "s/[^0-9]//g")
		SPACEMAX=$(echo "$SPACEMAXVALUE"| awk '{print $1}')
		#INODEMAXVALUE=$(echo $2 | awk -F ',' '{print $2}' | sed "s/[^0-9]//g")
		#INODEMAX=$(echo "$INODEMINVALUE"| awk '{print 100-$1}')
 	fi

}

function resultOutput(){
	if [[ "$StateDesStr" == "" ]];then
		echo "mailstatedes=指标恢复正常 statedes=指标恢复正常 |$DataResultStr totalspace=$TOTALSPACE"
	else
		echo "mailstatedes=$StateDesStr statedes=$MsgContent ${QUOTASTR}  quota=${quota} |$DataResultStr totalspace=$TOTALSPACE"
	fi
}

while getopts "w:c:p:h" arg
do
	case $arg in
		w)
			parseARG 1 "$OPTARG"
			;;
		c)
			parseARG 2 "$OPTARG"
			;;
		p)
			FLAGE=$OPTARG
			;;
		h)
			showHelp
			;;
		?) 
			showHelp
			;;
	esac
done

if [[ "$FLAGE" == "" ]];then
	showHelp "特征项"
fi

checkALL

if [[ $? == 1 ]];then
	showHelp "正确参数"
elif [[ $? == 2 ]];then
	showHelp "要监控项"
fi

#插件正题部分 
#确定存活的
#启动
#分析

analysisSpaceStat
analysisResult 1
TOTALSPACE=$(df -hP  | grep $FLAGE | head -n 1 | awk -F ' ' '{print $2}')
resultOutput
exit $ExitCode
